//
//  FilterView.m
//  IphoneImageProcessing
//
//  Created by Ngoc on 17/06/2011.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "FilterView.h"
#import "CoordinatingController.h"
#import "SwitchViewController.h"


@implementation FilterView
@synthesize Filter = filter_;
@synthesize originalImage;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
-(id) initWithFilter:(FilterAlgorithm)filter originalImage:(UIImage *)image returnView:(UIViewController *)view
{
    self = [super init];
    if(self)
    {
        filter_ = filter;
        originalImage = [image retain];
        returnView = view;
        
    }
    return self;
}
- (void)dealloc
{
    [originalImage release];
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    NSString * title;
    switch (filter_) {
        case TintRed:
        {
            title =   [[NSString alloc]initWithString:@"Tint Red"];
            break;
        }
        case Greyscale:
        {
            title =   [[NSString alloc]initWithString:@"Grey Scale"];
            break;
        }
        case Pixelate :
        {
            title =   [[NSString alloc]initWithString:@"Pixelate"];
            break;
        }
        case Brightness:
        {
            title =   [[NSString alloc]initWithString:@"Brightness"];
            break;
        }
        case Saturation:
        {
            title =   [[NSString alloc]initWithString:@"Saturation"];
            break;
        }
        case Hue :
        {
            title =   [[NSString alloc]initWithString:@"Hue"];
            break;
        }
        case Thermal:
        {
            title =   [[NSString alloc]initWithString:@"Thermal"];
            break;
        }
        case SnowFuzz:
        {
            title =   [[NSString alloc]initWithString:@"Snow Fuzz"];
            break;
        }
        case Blur:
        {
            title =   [[NSString alloc]initWithString:@"Blur"];
            break;
        }
        case FindEdges:
        {
            title =   [[NSString alloc]initWithString:@"Find Edges"];
            break;
        }
        case Emboss:
        {
            title =   [[NSString alloc]initWithString:@"Emboss"];
            break;
        }
        case Sharpen:
        {
            title =   [[NSString alloc]initWithString:@"Sharpen"];
            break;
        }
        case TiltShift :
        {
            title =   [[NSString alloc]initWithString:@"Tilt Shift"];
            break;
        }   
        default:
            break;
    }

    nagigationBar.topItem.title = title;
    [self applyEffect];
    // Do any additional setup after loading the view from its nib.
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
   // return (interfaceOrientation == UIInterfaceOrientationPortrait);
    return YES;
}
-(IBAction)returnPrevisousView
{
    [[SwitchViewController sharedInstance]switchView:self toView:returnView];
}
-(IBAction) applyEffectAndReturnResult
{
    [[[CoordinatingController sharedInstance]ediImageViewController] imageView].image = imageView.image;
    [self returnPrevisousView];
}
-(void) applyEffect
{
    slider.hidden = YES;
    switch (filter_) {
            case TintRed:
            {
                
                IFSimpleTintFilter *tinter = [[IFSimpleTintFilter alloc] initWithOriginalImage:originalImage];
                tinter.tintColor = [UIColor redColor];
                imageView.image = [tinter imageWithFilterApplied];
                [tinter release];
                
                break;
            }
            case Greyscale:
            {
                IFGreyscaleFilter *grey = [[IFGreyscaleFilter alloc] initWithOriginalImage:originalImage];
                imageView.image = [grey imageWithFilterApplied];
                [grey release];
                break;
            }
            case Pixelate :
            {
                slider.minimumValue = 1.0;
                slider.maximumValue = 20.0;
                slider.value = 1.0;
                sliderMode = IFFilterPreviewSliderModePixelate;
                slider.hidden = NO;
                [self sliderMoved:slider];
                break;
            }
            case Brightness:
            {
                slider.minimumValue = -150.0;
                slider.maximumValue = 150.0;
                slider.value = 0.0;
                sliderMode = IFFilterPreviewSliderModeBrightness;
                slider.hidden = NO;
                [self sliderMoved:slider];
                break;
            }
            case Saturation:
            {
                slider.minimumValue = -150.0;
                slider.maximumValue = 150.0;
                slider.value = 0.0;
                sliderMode = IFFilterPreviewSliderModeSaturation;
                slider.hidden = NO;
                [self sliderMoved:slider];
                break;
            }
            case Hue :
            {
                slider.minimumValue = 0;
                slider.maximumValue = 360.0;
                slider.value = 0.0;
                sliderMode = IFFilterPreviewSliderModeHue;
                slider.hidden = NO;
                break;
            }
            case Thermal:
            {
                IFThermalFilter *thermal = [[IFThermalFilter alloc] initWithOriginalImage:originalImage];
                imageView.image = [thermal imageWithFilterApplied];
                [thermal release];
                break;
            }
           case SnowFuzz:
            {
                IFSnowFuzzFilter *snow = [[IFSnowFuzzFilter alloc] initWithOriginalImage:originalImage];
                imageView.image = [snow imageWithFilterApplied];
                [snow release];
                break;
            }
            case Blur:
            {
                IFBlurFilter *blur = [[IFBlurFilter alloc] initWithOriginalImage:originalImage];
                imageView.image = [blur imageWithFilterApplied];
                [blur release];
                break;
            }
            case FindEdges:
            {
                IFFindEdgesFilter *edges = [[IFFindEdgesFilter alloc] initWithOriginalImage:originalImage];
                imageView.image = [edges imageWithFilterApplied];
                [edges release];
                break;
            }
            case Emboss:
            {
                IFEmbossFilter *emboss = [[IFEmbossFilter alloc] initWithOriginalImage:originalImage];
                imageView.image = [emboss imageWithFilterApplied];
                [emboss release];
                break;
            }
            case Sharpen:
            {
                IFSharpenFilter *sharpen = [[IFSharpenFilter alloc] initWithOriginalImage:originalImage];
                imageView.image = [sharpen imageWithFilterApplied];
                [sharpen release];
                break;
            }
            case TiltShift :
            {
                activityView.hidden = NO;
                
                IFTiltShiftFilter *tilt = [[IFTiltShiftFilter alloc] initWithOriginalImage:originalImage];
                tilt.delegate = self;
                //imageView.image = [tilt imageWithFilterApplied];
                [tilt applyFilterOnNewThread];
                [tilt release];
                break;
            }   
        default:
            break;
    }
}
- (IBAction) sliderMoved:(id)sender
{
	if(sliderMode == IFFilterPreviewSliderModePixelate)
	{
		IFPixelationFilter *pixels = [[IFPixelationFilter alloc] initWithOriginalImage:originalImage];
		pixels.pixelSize = roundf(slider.value);
		imageView.image = [pixels imageWithFilterApplied];
		[pixels release];
	}
	else if(sliderMode == IFFilterPreviewSliderModeBrightness)
	{
		IFBrightnessFilter *brightness = [[IFBrightnessFilter alloc] initWithOriginalImage:originalImage];
		brightness.brightnessAdjustment = roundf(slider.value);
		imageView.image = [brightness imageWithFilterApplied];
		[brightness release];
	}
	else if(sliderMode == IFFilterPreviewSliderModeSaturation)
	{
		IFSaturationFilter *saturation = [[IFSaturationFilter alloc] initWithOriginalImage:originalImage];
		saturation.saturationAdjustment = roundf(slider.value);
		imageView.image = [saturation imageWithFilterApplied];
		[saturation release];
	}
	
	else if(sliderMode == IFFilterPreviewSliderModeHue)
	{
		IFHueFilter *hue = [[IFHueFilter alloc] initWithOriginalImage:originalImage];
		hue.hueAdjustment = roundf(slider.value);
		imageView.image = [hue imageWithFilterApplied];
		[hue release];
	}
}
#pragma mark - Filter Delegate

- (void) filterDidApplyWithResult:(UIImage *)result
{
	activityView.hidden = YES;
	imageView.image = result;
}
@end
